﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using MasterDatabase;
using System.Collections;
using System.Globalization;
using System.Threading;
using LayoutControl;

namespace JobsDisplay
{
    public partial class Form1 : SQL_APPL
    {
        enum Tracking_Mode
        {
            VIEW = 0,
            TRACKING = 1,
        }
        public static string MasterDatabase_Connection_Str = @"server=(local)\SQLEXPRESS;database=JOB_ASSIGNMENT_DB;Integrated Security = TRUE";
        public static string LeaveRegister_Connection_Str = @"server=(local)\SQLEXPRESS;database=SHIFT_REGISTER_DB;Integrated Security = TRUE";
       

        private string Folder_Path = @"C:\ATC\Setting";
        private string Configure_FileName = "setting.bak";

        private Tracking_Mode Mode;
        public string Cur_Line_ID = "";
        public string Cur_WST_ID = "";
        private string Used_Port = "";
        private string Used_Baudrate = "";
        private string Used_Databit = "";
        private string Used_Parity = "";
        private string Used_Stopbit = "";
        
        public Form1()
        {
            CultureInfo culture;
            culture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            InitializeComponent();

            filterStatusLabel.Text = "";
            filterStatusLabel.Visible = false;
            showAllLabel.Text = "Show &All";
            showAllLabel.Visible = false;
            showAllLabel.IsLink = true;
            showAllLabel.LinkBehavior = LinkBehavior.HoverUnderline;
            Init_Configure();
        }

        public void Init_Configure()
        {
            COMPORT_INIT();
            Load_Configure_File();
            Init_Setting_Tab();
            Application.DoEvents();
            GetTab1SerialConfig();
            Application.DoEvents();
            Open_Logger_Comport();
        }

        public void Load_Configure_File()
        {
            string file_path = Folder_Path + "\\" + Configure_FileName;
            StreamReader myfile;
            StreamWriter writeStream;
            string strline;
            string mode_str;

            if (Directory.Exists(Folder_Path) == false)
            {
                Directory.CreateDirectory(Folder_Path);
            }
            if (File.Exists(file_path) == false)
            {
                writeStream = File.CreateText(file_path);
                writeStream.WriteLine("Mode: VIEW");
                writeStream.WriteLine("Line_ID");
                writeStream.WriteLine("WST_ID");
                writeStream.WriteLine("COM1");
                writeStream.WriteLine("9600");
                writeStream.WriteLine("8");
                writeStream.WriteLine("NONE");
                writeStream.WriteLine("ONE");
                writeStream.Close();
            }

            myfile = File.OpenText(file_path);

            try
            {
                strline = myfile.ReadLine();
                mode_str = strline.Split(':')[1].Trim();
                if (mode_str == "TRACKING")
                {
                    Mode = Tracking_Mode.TRACKING;
                }
                else
                {
                    Mode = Tracking_Mode.VIEW;
                }

                Cur_Line_ID = myfile.ReadLine().Trim();
                Cur_WST_ID = myfile.ReadLine().Trim();

                Used_Port = myfile.ReadLine().Trim();
                Used_Baudrate = myfile.ReadLine().Trim();
                Used_Databit = myfile.ReadLine().Trim();
                Used_Parity = myfile.ReadLine().Trim();
                Used_Stopbit = myfile.ReadLine().Trim();
                myfile.Close();
            }
            catch
            {
                myfile.Close();
                File.Delete(file_path);
                writeStream = File.CreateText(file_path);
                writeStream.WriteLine("Mode: VIEW");
                writeStream.WriteLine("Line_ID");
                writeStream.WriteLine("WST_ID");
                writeStream.WriteLine("COM1");
                writeStream.WriteLine("9600");
                writeStream.WriteLine("8");
                writeStream.WriteLine("NONE");
                writeStream.WriteLine("ONE");
                writeStream.Close();

                Mode = Tracking_Mode.VIEW;
                Cur_Line_ID = "Line_ID";
                Cur_WST_ID = "WST_ID";
                Used_Port = "COM1";
                Used_Baudrate = "9600";
                Used_Databit = "8";
                Used_Parity = "NONE";
                Used_Stopbit = "ONE";
            }
        }


        private void Save_Configure_File()
        {
            string file_path = Folder_Path + "\\" + Configure_FileName;
            StreamWriter writeStream;;

            if (Directory.Exists(Folder_Path) == false)
            {
                Directory.CreateDirectory(Folder_Path);
            }
            if (File.Exists(file_path) == true)
            {
                File.Delete(file_path);
            }

            writeStream = File.CreateText(file_path);
            if (Mode == Tracking_Mode.TRACKING)
            {
                writeStream.WriteLine("Mode: TRACKING");
            }
            else
            {
                writeStream.WriteLine("Mode: VIEW");
            }
            writeStream.WriteLine(Cur_Line_ID);
            writeStream.WriteLine(Cur_WST_ID);
            writeStream.WriteLine(Used_Port);
            writeStream.WriteLine(Used_Baudrate);
            writeStream.WriteLine(Used_Databit);
            writeStream.WriteLine(Used_Parity);
            writeStream.WriteLine(Used_Stopbit);
            writeStream.Close();
        }

        private void Setting_Save_BT_Click(object sender, EventArgs e)
        {
            Save_Configure_File();
        }

        private void Process_inData(string indata)
        {
            if (Mode == Tracking_Mode.VIEW)
            {
                tabControl1.SelectTab(YourJobs_TabPage);
                // Show_Plan(indata);
                YourJob_MSNV_Txt.Text = "";
                Application.DoEvents();
                YourJob_MSNV_Txt.Text = indata;
            }
            else
            {
                tabControl1.SelectTab(Tracking_Tab);
                Tracking_MSNV_Txt.Text = "";
                Application.DoEvents();
                Tracking_MSNV_Txt.Text = indata;

                //Create_Tracking(indata);
                //Load_WST_Tracking(Cur_Line_ID, Cur_WST_ID, DateTime.Now);
                //BindingSource bs = new BindingSource();
                //bs.DataSource = WST_Tracking_dtb;
                //Tracking_GridView.DataSource = bs;
                //Tracking_GridView_BindingContextChanged(null, null);
            }
        }

        private void trackingViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tracking_View_Init();
        }

        private void Port_Close_BT_Click(object sender, EventArgs e)
        {
            Close_Logger_Comport();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetTab1SerialConfig();
            Open_Logger_Comport();
        }

        private void designToolToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMaster frm = new frmMaster();
            frm.ShowDialog();
        }

        private void databaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LineLayout_Init();
        }
    }
}
